﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2023/12/10
 * 时间: 16:53
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Drawing;

using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

namespace midifyTkInfos08
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	public partial class ModifyTkInfoForm : Form
	{
		public ModifyTkInfoForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		void ModifyTkInfoFormLoad(object sender, EventArgs e)
		{
			checkBox1.Checked = false;
			checkBox2.Checked = false;
			checkBox3.Checked = false;
			checkBox4.Checked = false;
			checkBox5.Checked = false;
			checkBox6.Checked = false;
			checkBox7.Checked = false;

			textBox2.Enabled = false;
			textBox3.Enabled = false;
			textBox4.Enabled = false;
			textBox5.Enabled = false;
			textBox6.Enabled = false;
			textBox7.Enabled = false;
			textBox8.Enabled = false;
			
		}
		void Button1Click(object sender, EventArgs e)
		{
			System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();

			System.Windows.Forms.DialogResult dialogResult = folderBrowserDialog.ShowDialog();

			if (dialogResult != System.Windows.Forms.DialogResult.OK)
			{
				return;
			}
			textBox1.Text = folderBrowserDialog.SelectedPath;

			listBox1.Items.Clear();

			foreach (var item in System.IO.Directory.GetFiles(textBox1.Text,"*.dwg"))
			{
				listBox1.Items.Add(item);
			}


			
		}
		void Button2Click(object sender, EventArgs e)
		{
			this.Hide();

			string[] dwgFiles = System.IO.Directory.GetFiles(textBox1.Text,"*.dwg");
			
			try
			{
				Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

				foreach (string drawingFilePath in dwgFiles)
				{
					using (Database database = new Database(false, true))
					{

						#if CadVersion08
						
						database.ReadDwgFile(drawingFilePath, System.IO.FileShare.ReadWrite, true, "");
						
						#elif CadVersion13

						database.ReadDwgFile(drawingFilePath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);

						#endif
						
						database.CloseInput(true);

						using (Transaction transaction = database.TransactionManager.StartTransaction())
						{

							var bt = database.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;

							foreach (var idBtr in bt)
							{
								var btr = idBtr.GetObject(OpenMode.ForWrite) as BlockTableRecord;
								if (btr.Name == "B-SZDJB-A3")
								{
									foreach (var oid in btr)
									{
										Entity ent = transaction.GetObject(oid, OpenMode.ForWrite) as Entity;
										ent.ColorIndex = 3;
									}
								}
							}

							//打开数据库的模型空间块表记录对象
							BlockTableRecord modelbtr = (BlockTableRecord)transaction.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
							
							//循环遍历模型空间中的实体
							foreach (ObjectId id in modelbtr)
							{
								BlockReference block_Ref = transaction.GetObject(id, OpenMode.ForWrite) as BlockReference;

								if (block_Ref != null)
								{
									if (block_Ref.AttributeCollection.Count > 0)
									{
										foreach (ObjectId att_id in block_Ref.AttributeCollection)
										{
											AttributeReference attRef = transaction.GetObject(att_id, OpenMode.ForWrite) as AttributeReference;
											if (attRef.Tag.ToString().Contains("日期"))
											{
												attRef.TextString = "20300.12";
											}
											if (checkBox1.Checked==true)
											{
												if (attRef.Tag.ToString().Contains("工程名称"))
												{
													attRef.TextString = textBox2.Text;
												}
											}
											if (checkBox2.Checked == true)
											{
												if (attRef.Tag.ToString().Contains("项目名称"))
												{
													attRef.TextString = textBox3.Text;
												}
											}

											if (checkBox3.Checked == true)
											{
												if (attRef.Tag.ToString().Contains("版本"))
												{
													attRef.TextString = textBox4.Text;
												}
											}

											if (checkBox4.Checked == true)
											{
												if (attRef.Tag.ToString().Contains("工号"))
												{
													attRef.TextString = textBox5.Text;
												}
											}

											if (checkBox5.Checked == true)
											{
												if (attRef.Tag.ToString().Contains("阶段"))
												{
													attRef.TextString = textBox6.Text;
												}
											}

											if (checkBox6.Checked == true)
											{
												if (attRef.Tag.ToString().Contains("专业"))
												{
													attRef.TextString = textBox7.Text;
												}
											}
											if (checkBox7.Checked == true)
											{
												if (attRef.Tag.ToString().Contains("日期"))
												{
													attRef.TextString = textBox8.Text;
												}
											}

										}
									}
								}
							}

							//打开数据库的图纸空间块表记录对象
							BlockTableRecord paperBtr = (BlockTableRecord)transaction.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);

							//循环遍历模型空间中的实体
							foreach (ObjectId id in paperBtr)
							{
								BlockReference block_Ref = transaction.GetObject(id, OpenMode.ForWrite) as BlockReference;
								if (block_Ref != null)
								{
									if (block_Ref.AttributeCollection.Count > 0)
									{
										foreach (ObjectId att_id in block_Ref.AttributeCollection)
										{
											AttributeReference attRef = transaction.GetObject(att_id, OpenMode.ForWrite) as AttributeReference;
											if (attRef.Tag.ToString().Contains("日期"))
											{
												attRef.TextString = "20300.12";
											}
										}
									}
								}
							}
							
							//DO STUFF HERE
							transaction.Commit();

						}
						
						#if CadVersion08
						
						database.SaveAs(drawingFilePath,DwgVersion.Current);
						
						#elif CadVersion13

						database.SaveAs(drawingFilePath, true, DwgVersion.AC1021, null);

						#endif
						
					}
				}

			}
			catch (System.Exception)
			{

//				throw;
			}
			
			
			Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("everyThingIsOk");
			
			this.Close();
		}
		void CheckBox1CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox1.Checked==true)
			{
				textBox2.Enabled = true;
			}
			else
			{
				textBox2.Enabled = false;
			}
		}
		void CheckBox2CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox2.Checked == true)
			{
				textBox3.Enabled = true;
			}
			else
			{
				textBox3.Enabled = false;
			}
		}
		void CheckBox3CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox3.Checked == true)
			{
				textBox4.Enabled = true;
			}
			else
			{
				textBox4.Enabled = false;
			}
		}
		void CheckBox4CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox4.Checked == true)
			{
				textBox5.Enabled = true;
			}
			else
			{
				textBox5.Enabled = false;
			}
		}
		void CheckBox5CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox5.Checked == true)
			{
				textBox6.Enabled = true;
			}
			else
			{
				textBox6.Enabled = false;
			}
		}
		void CheckBox6CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox6.Checked == true)
			{
				textBox7.Enabled = true;
			}
			else
			{
				textBox7.Enabled = false;
			}
		}
		void CheckBox7CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox7.Checked == true)
			{
				textBox8.Enabled = true;
			}
			else
			{
				textBox8.Enabled = false;
			}
		}
		void TextBox1TextChanged(object sender, EventArgs e)
		{
	
		}
		
		
	}
}
